package com.dc.system.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dc.base.exception.BusinessException;
import com.dc.base.util.AESUtil;
import com.dc.common.constants.ConstantsCommon;
import com.dc.common.model.BaseModel;
import com.dc.system.controller.UserController;
import com.dc.system.mapper.UserMapper;
import com.dc.system.model.SysLoginLog;
import com.dc.system.model.SysRolePermission;
import com.dc.system.model.SysUser;
import com.dc.system.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserServiceImpl implements UserService {

	private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	@Autowired
	private UserMapper userMapper;

//	@Autowired
//	private LoginLogMapper loginLogMapper;

	@Override
	public BaseModel login(BaseModel baseModel, SysLoginLog sysLoginLog, SysUser sysUser,
			HttpServletRequest request) throws Exception {
		if (sysUser.getAccount() == "" || "".equals(sysUser.getAccount())) {
			throw new BusinessException(-101, "用户账号不能为空！");
		}
		if (sysUser.getPassword() == "" || "".equals(sysUser.getPassword())) {
			throw new BusinessException(-102, "密码不能为空！");
		}
		//密码加密
		sysUser.setPassword(AESUtil.encrypt(sysUser.getPassword()));
		//根据用户账号以及密码获取用户信息
		SysUser user_select = userMapper.getUserByAccountAndPwd(sysUser);
		
		// 判断用户是否存在
		if (user_select == null) {
			throw new BusinessException(-104, "请输入正确的用户名和密码");
		} else {
			baseModel.setMessage("登陆成功");
			baseModel.setCode(0);
			baseModel.setData(user_select);
		}
		
		//把用户信息放入session中
		request.getSession().setAttribute(ConstantsCommon.LOGIN_INFO, baseModel.getData());
		
		//拼装登陆信息
//		sysLoginLog.(request.getRemoteAddr());
//		systemLoginLog.setUser_id(user_select.getUser_id());
//		systemLoginLog.setState(1);
//		systemLoginLog.setUser_name(user_select.getUser_name());
//		systemLoginLog.setSession_id(request.getSession().getId());
//		systemLoginLog.setUser_name(user_select.getUser_name());
//		systemLoginLog.setSession_id(request.getSession().getId());
//		
//		// 插入数据库登陆日志
//		loginLogMapper.insertLoginLog(systemLoginLog);
		
		return baseModel;
	}

	@Override
	public BaseModel getUserPermission(BaseModel baseModel,HttpServletRequest request) {
		SysUser systemUser = (SysUser) request.getSession().getAttribute(ConstantsCommon.LOGIN_USERINFO);
		// 分页插件,但是PageHelper必须在查询语句前，最好写在service实现层
		//PageHelper.startPage(1, 10);
		List<SysRolePermission> SysRole_Permissions = userMapper.getUserPermission(systemUser);
		// 通过pageHelper插件pageInfo封装，更加方便
		//PageInfo<SysRole_Permission> pageInfo = new PageInfo<SysRole_Permission>(SysRole_Permissions);
		baseModel.setData(SysRole_Permissions);
		//测试操作日志
		baseModel.setAopMsg("测试操作日志自定义！");
		return baseModel;
	}

}
